﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TracyGUI.UIComponent;
using Tracy.DataServices;
using Tracy;
using System.Windows.Forms;

namespace TracyGUI
{
    public partial class FormMain
    {
        TabPageEntry _searchEntryPage = null;
        
        private void toolStripTextBoxSearch_Click(object sender, EventArgs e)
        {
            toolStripTextBoxSearch.SelectAll();
        }

        private void toolStripTextBoxSearch_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 13)
            {
                PerformSearch();
            }
        }

        private void toolStripTextBoxSearch_TextChanged(object sender, EventArgs e)
        {
            PerformSearch();
        }

        private void PerformSearch()
        {
            if (String.IsNullOrEmpty(toolStripTextBoxSearch.Text) && toolStripTextBoxSearch.Text == "Type to Search") return;
            TracyFacade.Instance.SearchEngine.Search(toolStripTextBoxSearch.Text);//TODO: Use ascyn call
            if (_searchEntryPage == null)
            {
                _searchEntryPage = CreateEntryTab(TabPageEntry.TABPAGE_NAME_SEARCH, toolStripTextBoxSearch.Text);
                _tabControlContent.TabPages.Add(_searchEntryPage);
            }
            else
            {
                LoadDataToEntryTab(_searchEntryPage, TabPageEntry.TABPAGE_NAME_SEARCH, toolStripTextBoxSearch.Text, true);
            }

            _tabControlContent.SelectedTab = _searchEntryPage;
            toolStripTextBoxSearch.Focus();
        }
    }
}
